package cn.wawi.controller.sys;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.wawi.common.ResultData;
import cn.wawi.controller.BaseController;
import cn.wawi.dao.sys.PrivilegeDao;
import cn.wawi.dao.sys.UserDao;
import cn.wawi.entity.sys.Privilege;
import cn.wawi.entity.sys.User;
import com.wawi.utils.StringEscapeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jfinal.kit.StrKit;

@Controller
@RequestMapping("/sys_user")
public class UserController extends BaseController<User>{
	@Resource
	protected UserDao<User> userDao;
	@Resource
	protected PrivilegeDao<Privilege> privilegeDao;
	
	/*@InitBinder({"department"})
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("department.");
	}*/
	/**
	 * 用户退出
	 */
	@RequestMapping(value="/exit",method=RequestMethod.GET)
	public String exit(HttpSession httpSession){
		httpSession.removeAttribute("loginUser");
		httpSession.invalidate();
		return "core/login";
	}
	/**
	 * 修改密码
	 */
	@ResponseBody
	@RequestMapping(value="/updatePassword",method=RequestMethod.POST)
	public ResultData<User> updatePassword(HttpSession httpSession,String password){
		ResultData<User> result=new ResultData<User>();
		User user=(User) httpSession.getAttribute("loginUser");
		if(user!=null&&StrKit.notBlank(password)){
			user.setPassword(StringEscapeUtil.md5(password));
			httpSession.setAttribute("loginUser", user);
		}
		userDao.updateOne(user);
		return result;
	}
	/**
	 * 用户登陆
	 */
	@RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET})
	public String login(HttpSession session,HttpServletRequest request,User user){
		User loginUser=(User) session.getAttribute("loginUser");
		if(loginUser==null){
//	        if(request.getParameter("captcha")==null||!request.getParameter("captcha").equalsIgnoreCase((String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY))){
//	        	request.setAttribute("msg", "验证码错误!");
//	        	return "login";
//	        }else{
//				if(user!=null)loginUser=userDao.findOne(user);
//				if(loginUser==null){
//					request.setAttribute("msg", "用户名或密码不对!");
//					return "login";
//				}
//	        }
			if(user!=null&&StrKit.notBlank(user.getUsername(),user.getPassword())){
				loginUser=userDao.findOne(paramaterMap);
			}
			if(loginUser==null){
				request.setAttribute("msg", "用户名或密码不对!");
				return "core/login";
			}
		}
        session.setAttribute("loginUser", loginUser);
        loginUser.setStatus("F");
		List<Privilege> list=privilegeDao.findUserPermission(loginUser);
		request.setAttribute("list", list);
		return "core/index";
	}
	/**
	 *主页
	 */
	@RequestMapping(value="/main",method=RequestMethod.GET)
	public String main() {
		return "sys/User/main";
	}
	
}
